package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库工具类
 */
public class DbUtil {
    private String dburl="jdbc:mysql://localhost:3306/db_book";
    private String name="root";
    private String password="13949271904";
    private String jdbcName="com.mysql.cj.jdbc.Driver";

    /**
     * 获取数据库连接
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public Connection getCon() throws ClassNotFoundException, SQLException {
        Class.forName(jdbcName);
        Connection connection= DriverManager.getConnection(dburl,name,password);
        return connection;
    }

    /**
     * 关闭数据库
     * @param connection
     * @throws SQLException
     */
    public void closeCon(Connection connection) throws SQLException {
        if(connection!=null){
            connection.close();
        }
    }

}
